package com.situ.mall.controller.front;

import java.nio.channels.NonWritableChannelException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Category;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.OrderItem;
import com.situ.mall.entity.Product;
import com.situ.mall.entity.Province;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.Shipping;
import com.situ.mall.entity.User;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IOrderService;
import com.situ.mall.service.IProductService;
import com.situ.mall.service.IShippingService;
import com.situ.mall.service.IUserService;

@Controller
@RequestMapping("/userInfo")
public class FrontUserInfoController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IShippingService shippingService;
	@Autowired
	private ICategoryService categoryService;

	//进入用户信息界面
	@RequestMapping("/getUserInfoPage.shtml")
	public String getUserInfoPage(HttpSession session, Model model) {
		System.out.println("FrontUserOrderController.getUserInfoPage()");
		User user = (User)session.getAttribute("user");
		System.out.println("User = " + user);
		
		//查找user信息
		User userNow = userService.selectByUserId(user.getId());
		//System.out.println(userNow);
		//查找收货地址
		List<Shipping> shippingList = shippingService.selectByUserId(user.getId()); 
		System.out.println(shippingList);
		
		List<Category> topCategoryList = categoryService.selectTopCategoryList();
		List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
		model.addAttribute("topCategoryList", topCategoryList);
		model.addAttribute("secondCategoryList", secondCategoryList);
		
		model.addAttribute("shippingList", shippingList);
		model.addAttribute("userNow", userNow);
		return "user_info";
	}
	
	//进入修改地址页面
	@RequestMapping("/getUpdateAddress.shtml")
	public String getUpdateAddress(HttpSession session, Model model,Integer id) {
		System.out.println("FrontUserOrderController.getUpdateAddress()");
		User user = (User)session.getAttribute("user");
		System.out.println("User = " + user);
		System.out.println("id = " + id);
		
		//查找
		Shipping shipping = shippingService.selectById(id);
		//System.out.println("shipping = " + shipping);
		
		model.addAttribute("shipping", shipping);
		return "user_updateAddress";
	}

	//进入修改地址页面
	@RequestMapping("/getAddAddress.shtml")
	public String getAddAddress(HttpSession session, Model model,Integer id) {
		System.out.println("FrontUserOrderController.getAddAddress()");

		return "user_addAddress";
	}
	
	
	
	//进入修改用户信息界面
	@RequestMapping("/getUpdateUserInfo.shtml")
	public String getUpdateUserInfo(HttpSession session, Model model) {
		System.out.println("FrontUserOrderController.getUserInfoPage()");
		User user = (User)session.getAttribute("user");
		System.out.println("User = " + user);
		
		User userNow = userService.selectByUserId(user.getId());
		model.addAttribute("userNow", userNow);
		return "user_updateInfo";
	}

	//修改用户信息
	@RequestMapping("/updateUserInfo.shtml")
	@ResponseBody
	public Result updateUserInfo(User user) {
		System.out.println(user);
		boolean isSuccess = userService.updateUserInfo(user);
		if (isSuccess) {
			return Result.success("修改成功");
		} else {
			return Result.error("修改失败");
		}
	}
	
	//修改地址shipping
	@RequestMapping("/updateAddress.shtml")
	@ResponseBody
	public Result updateAddress(Shipping shipping) {
		System.out.println(shipping);
		boolean isSuccess = shippingService.updateAddress(shipping);
		if (isSuccess) {
			return Result.success("修改收货地址成功");
		} else {
			return Result.error("修改收货地址失败");
		}
	}
	
	//删除地址shipping
	@RequestMapping("/DeleteAddress.shtml")
	@ResponseBody
	public Result deleteAddress(Integer id) {
		System.out.println(id);
		boolean isSuccess = shippingService.deleteAddress(id);
		if (isSuccess) {
			return Result.success("删除收货地址成功");
		} else {
			return Result.error("删除收货地址失败");
		}
	}

	//新增地址shipping
	@RequestMapping("/AddAddress.shtml")
	@ResponseBody
	public Result AddAddress(Shipping shipping,HttpSession session) {
		System.out.println("addshipping = " + shipping);
		User user = (User)session.getAttribute("user");
		shipping.setUserId(user.getId());
		boolean isSuccess = shippingService.AddAddress(shipping);
		if (isSuccess) {
			return Result.success("成功新增地址");
		} else {
			return Result.error("新增地址失败");
		}
	}
	
	//查找用户密码，匹配则修改
	@RequestMapping("/selectUserPass.shtml")
	@ResponseBody
	public Result selectUserPass(String oldPass,String newPass1,String newPass2,HttpSession session) {
		System.out.println("FrontUserInfoController.selectUserPass()");
		User user = (User)session.getAttribute("user");
		User userNow = userService.selectByUserId(user.getId());
		String userPass = userNow.getPassword();
		System.out.println("userPass = " + userPass);
		System.out.println("oldPass = " + oldPass);
		
		if (userPass.equals(oldPass)) {
			//执行修改
			userNow.setPassword(newPass1);
			System.out.println(userNow.getPassword());
			boolean isSuccess = userService.updateUserInfo(userNow);
			if (isSuccess) {
				return Result.success("修改成功,新密码为"+newPass1);
			} else {
				return Result.error("修改失败");
			}
		}else {
			return Result.error("密码错误");
		}
	}		
	
	//注册页面
	@RequestMapping("/getRegister.shtml")
	public String getRegister() {
		return "user_register";
	}
	
	//进行注册
	@RequestMapping("/register.shtml")
	@ResponseBody
	public Result register(User user) {
		System.out.println("UserController.register()");
		System.out.println(user);
		
		User selectUser = userService.registerByUserName(user.getUsername());
		System.out.println("selectUser = " + selectUser);
		
		if(selectUser == null) {
			user.setRole(1);
			boolean isSuccess = userService.register(user);
			if (isSuccess) {
				return Result.success("注册成功");
			} else {
				return Result.error("注册失败");
			}
		}else {
			return Result.error("用户名“"+selectUser.getUsername()+"”重名，不可修改");
			
		}
		
	}
	
	//退出用户
	@RequestMapping("/ExitUser.shtml")
	@ResponseBody
	public Result ExitUser(HttpSession session)
    {
		User user = (User)session.getAttribute("user");
        session.invalidate();
		return Result.success("已退出当前用户");
    }
}
